package com.net.xpay.common.manager.impl;

import com.google.common.base.Preconditions;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.CheckCodeMapper;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.core.model.CheckCodeSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@ReadTransactional
public class CheckCodeManagerImpl extends AbstractManager<CheckCode> implements CheckCodeManager{

    @Autowired
    private CheckCodeMapper mapper;


    @Override
    protected BasicMapper<Long, CheckCode> getMapper() {
        return mapper;
    }

    @Override
    public CheckCode findByMobileAndType(String mobile, CheckCodeType checkCodeType) {
        Preconditions.checkArgument(mobile != null && checkCodeType != null);
        CheckCode condition = new CheckCode();
        condition.setMobile(mobile);
        condition.setType(checkCodeType);

        return getByCondition(condition);
    }

    @Override
    public CheckCode findLastByRelatedId(String mobile, CheckCodeType checkCodeType, Long relatedId) {
        Preconditions.checkArgument(mobile != null && checkCodeType != null && relatedId != null);
        CheckCode condition = new CheckCode();
        condition.setMobile(mobile);
        condition.setType(checkCodeType);
        condition.setRelatedId(relatedId);

        return mapper.findLastByCondition(condition);
    }

    @Override
    public List<CheckCode> search(CheckCodeSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(CheckCodeSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }
}
